package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 前台数据查询接口
 */
@Api(description = "前台查询数据管理")
@RestController
@RequestMapping("/eduservice/banner")
//@CrossOrigin
public class IndexController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduTeacherService teacherService;


    @ApiOperation("获取热门课程和讲师")
    @GetMapping("index")
    @Cacheable(key = "'selectIndexList'",value = "banner") //缓存
    public R index(){
        //获取前8条热门课程
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 8");
        List<EduCourse> courseList = courseService.list(queryWrapper);

        //获取前4名讲师
        QueryWrapper<EduTeacher> query = new QueryWrapper<>();
        query.orderByDesc("id");
        query.last("limit 4");
        List<EduTeacher> teacherList = teacherService.list(query);
        return R.ok().data("courseList",courseList).data("teacherList",teacherList);
    }
}
